package com.snatch.deal.shop.web;

import com.snatch.deal.shop.biz.service.ShopOrderService;
import com.snatch.deal.shop.biz.util.ConstantUtil;
import com.snatch.deal.shop.common.annotations.RateLimit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by hao.g on 18/5/15.
 */
@RestController
public class ShopOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShopOrderController.class);

    @Autowired
    private ShopOrderService shopOrderService;

    @RequestMapping("/seckill")
    @RateLimit
    public String index(Long stockId) {
        try{
            if (shopOrderService.createOrder(stockId)){
                LOGGER.info(ConstantUtil.SNATCH_DEAL_SUCCESS);
                return ConstantUtil.SNATCH_DEAL_SUCCESS;
            }
        } catch (Exception e){
            LOGGER.error(e.getMessage());
            return e.getMessage();
        }

        return ConstantUtil.SYSTEM_EXCEPTION;
    }
}
